﻿package tools;

import tools.Outter.StaticInner;


public class InnerClassDemo {
	public static void main(String[] args) {
		Outter outter = new Outter();
		// 不能直接创建内部类，她依赖于外部类
		// Inner inner=new Inner();
		Outter.Inner oi = outter.new Inner();
		// 直接创建静态内部类，她不依赖于外部类;
		StaticInner si = new StaticInner();
		System.out.println("---------innerclass outof method-------------");
		outter.getV();
		oi.getOutterV();
		si.getOutterV();
		System.out.println("---------innerclass in method-------------");
		outter.normalMethod();
		Outter.staticMethod();
	}

}

class Outter {

	private String str = "non-static-variable";
	private static String strstatic = "static-variable";
	public void getV() {
		System.out.println("outterclass is using " + str);
		System.out.println("outterclass is using " + strstatic);
	}

	// 一般内部类,可以有修饰符
	public class Inner {
		public void getOutterV() {
			System.out.println("innerclass is using " + str);
			System.out.println("innerclass is using " + strstatic);
		}
	}

	// 静态内部类,可以有修饰符
	public static class StaticInner {
		public void getOutterV() {
			// 静态内部类不能调用外部非静态成员
			// System.out.println(str);
			// 静态内部类可以直接调用外部静态成员
			System.out.println("staticinnerclass is using " + strstatic);
		}
	}

	public void normalMethod() {
		final String strnor = "strnor";
		// 方法中的内部类，不能有修饰符,只能是abstract或final,感觉就是一个局部变量
		class NMInner {
			public void getNMV() {
				System.out.println("normalmethod is using "+strnor);
				System.out.println("normalmethod is using "+str);
				System.out.println("normalmethod is using "+strstatic);
			}
		}
		NMInner nmi=new NMInner();
		nmi.getNMV();
	}

	public static void staticMethod() {
		final String strsm = "strsm";
		// 静态方法中的内部类，不能有修饰符，只能是abstract或final
		class SMInner {
			public void getSMV() {
				System.out.println("staticmethod is using "+strsm);
				// 静态内部类不能调用外部非静态成员
				// System.out.println(str);
				System.out.println("staticmethod is using "+strstatic);
			}
		}
		SMInner smi=new SMInner();
		smi.getSMV();
	}

}
